﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class EquipmentAllocateService : BaseService, IEquipmentAllocateService
    {
        private readonly IEquipmentAllocateRepository _equipmentAllocateRepository;
        private readonly IUnitOfWork _unitOfWork;
        public EquipmentAllocateService(IMapper mapper, IEquipmentAllocateRepository _equipmentAllocateRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._equipmentAllocateRepository = _equipmentAllocateRepository;
            this._unitOfWork = _unitOfWork;
        }

        public bool AddEquipmentAllocate(EquipmentAllocate equipmentAllocate)
        {
            _equipmentAllocateRepository.AddEntity(equipmentAllocate);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelEquipmentAllocate(long id)
        {
            var cust = _equipmentAllocateRepository.QueryEntityById(id);
            if (cust !=null)
            {
                _equipmentAllocateRepository.DelEntity(cust);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<EquipmentAllocate> GetEquipmentAllocate(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var custList = _equipmentAllocateRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                custList = custList.Where(c => c.Name.Contains(name));
            }
            var list = _equipmentAllocateRepository.GetEntityAlls(custList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public EquipmentAllocate? GetEquipmentAllocateById(long id)
        {
            return _equipmentAllocateRepository.QueryEntityById(id);
        }

        public bool UpdEquipmentAllocate(EquipmentAllocate equipmentAllocate)
        {
            var aobj = _equipmentAllocateRepository.QueryIsEntityWhere(x => x.Id == equipmentAllocate.Id);
            if (aobj == null)
            {
                return false;
            }
            _equipmentAllocateRepository.UpdEntity(equipmentAllocate);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
